<?php
defined('_JEXEC') or die;

require_once JPATH_COMPONENT.'/controller.php';
jimport('joomla.application.component.controllerform');

class BaseControllerComunas extends JControllerForm 
{

	public function datos_grid(){
	 	
		$model	= $this->getModel('Comunas');
		$model->datos_grid();
	 	exit;
 	
	}

	public function eliminar(){
		$model	= $this->getModel('Comunas');
		$cad = JRequest::getVar('cad');
		
		if($model->eliminar($cad))
		{
			?>
			<img src="<?php echo JURI::root(true).DS."components".DS."com_base".DS."icons".DS."spinner.gif"; ?>" style="display: none" 
			onload="window.parent.refrescar_grid_comunas(); window.parent.SqueezeBox.close();"
			></img> Comunas Eliminada Satisfactoriamente
			<?php
		}else{
			echo "Error al ELiminar la Comuna";
		}
		exit;
	} 	
	
 	public function guardar()
	{
		// Check for request forgeries.
		JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

		// Initialise variables.
		$app	= JFactory::getApplication();
		$model	= $this->getModel('Comunas');

		// Get the user data.
		$requestData = JRequest::getVar('jform', array(), 'post', 'array');

		// Validate the posted data.
		$form	= $model->getForm();
		if (!$form) {
			JError::raiseError(500, $model->getError());
			return false;
		}
		
		$data	= $model->validate($form, $requestData);

		// Check for validation errors.
		if ($data === false) {
			// Get the validation messages.
			$errors	= $model->getErrors();

			// Push up to three validation messages out to the user.
			for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) {
				if ($errors[$i] instanceof Exception) {
					$app->enqueueMessage($errors[$i]->getMessage(), 'warning');
				} else {
					$app->enqueueMessage($errors[$i], 'warning');
				}
			}

			// Save the data in the session.
			$app->setUserState('com_base.comunas.data', $requestData);

			// Redirect back to the registration screen.
			$this->setRedirect(JRoute::_('index.php?option=com_base&view=comunas&tmpl=component', false));
			return false;
		}

		// Attempt to save the data.
		$return	= $model->guardar($data);

		// Check for errors.
		if ($return === false) {
			// Save the data in the session.
			$app->setUserState('com_base.comunas.data', $data);

			// Redirect back to the edit screen.
			$this->setMessage(JText::sprintf('Fallo la insercion del registro', $model->getError()), 'warning');
			$this->setRedirect(JRoute::_('index.php?option=com_base&view=comunas&tmpl=component', false));
			return false;
		}

		// Flush the data from the session.
		$app->setUserState('com_base.comunas.data', null);

		?>
		<img src="<?php echo JURI::root(true).DS."components".DS."com_base".DS."icons".DS."spinner.gif"; ?>" style="display: none" 
		onload="window.parent.refrescar_grid_comunas(); window.parent.SqueezeBox.close();"
		></img>
		<?php

	}

	public function select(){
		$model	= $this->getModel('comunas');
		$idciudad = JRequest::getVar('idciudad');
		$onchange = JRequest::getVar('onchange');
		if($onchange) $onchange.="();";
		
		$options[] = JHTML::_('select.option','','Seleccione','idcomuna','comuna');
		$options= array_merge( $options, $model->obtenerDatos($idciudad,null));
		$return= "<dd>".JHTML::_('select.genericlist',$options,'jform[idcomuna]', 'class="required invalid" onchange="'.$onchange.'" aria-required="true" required="required" aria-invalid="true" ','idcomuna','comuna','','jform_idcomuna')."</dd>";
		echo $return;
		exit;
	}
		
}